/**
 * @description 数据模型入口文件
 * @author LI
 */

const User = require('./User')
const Account = require('./Account')
const GoodsInfo = require('./GoodsInfo')
const GoodsPhoto = require('./GoodsPhoto')
const Product = require('./Product')
const DecoratItem = require('./DecoratItem')
const DecoratItemInfo = require('./DecoratItemInfo')
const AccessRecord = require('./AccessRecord')
const RechargeRecord = require('./RechargeRecord')
const AcceptanceInfo = require('./AcceptanceInfo')
const HouseInfo = require('./HouseInfo')

// belongsTo 通过前面的数据表查询，带出来后面的数据表里面的数据 前面的数据表是设置外键，后面的数据表是主表
// 创建外键 GoodsPhoto.goodsInfoId -> GoodsInfo.id
// 一对一hasOne 一对多 hasMany 一对多 belongsTo 多对多belongsToMany
GoodsPhoto.belongsTo(GoodsInfo, {
  foreignKey: 'goodsInfoId'
})

Product.belongsTo(GoodsInfo, {
  foreignKey: 'goodsInfoId'
})

DecoratItem.belongsTo(GoodsInfo, {
  foreignKey: 'goodsInfoId'
})

DecoratItemInfo.belongsTo(GoodsInfo, {
  foreignKey: 'goodsInfoId'
})

DecoratItemInfo.belongsTo(DecoratItem, {
  foreignKey: 'decoratItemId'
})

Product.belongsTo(GoodsPhoto, {
  foreignKey: 'goodsPhotoId'
})

AccessRecord.belongsTo(Account, {
  foreignKey: 'linkUserId'
})

RechargeRecord.belongsTo(Account, {
  foreignKey: 'linkUserId'
})

HouseInfo.belongsTo(Account, {
  as: 'houseOwner',
  foreignKey: 'houseOwnerId'
})

HouseInfo.belongsTo(Account, {
  as: 'supervision',
  foreignKey: 'supervisionId'
})

HouseInfo.belongsTo(Account, {
  as: 'foreman',
  foreignKey: 'foremanId'
})

HouseInfo.belongsTo(Account, {
  as: 'bricklayer',
  foreignKey: 'bricklayerId'
})

HouseInfo.belongsTo(Account, {
  as: 'carpenter',
  foreignKey: 'carpenterId'
})

HouseInfo.belongsTo(Account, {
  as: 'plumber',
  foreignKey: 'plumberId'
})

HouseInfo.belongsTo(Account, {
  as: 'painter',
  foreignKey: 'painterId'
})

AcceptanceInfo.belongsTo(Account, {
  foreignKey: 'workerAccountId'
})

AcceptanceInfo.belongsTo(HouseInfo, {
  foreignKey: 'houseInfoId'
})

// hasMany 通过查询前面的数据表，带出来后面表里面的数据，后面的数据表设置外键，前面的是主表
// 创建外键 GoodsPhoto.goodsPhotoId -> Product.id
GoodsPhoto.hasMany(Product, {
  foreignKey: 'goodsPhotoId'
})

GoodsInfo.hasMany(Product, {
  foreignKey: 'goodsInfoId'
})

GoodsInfo.hasMany(DecoratItem, {
  foreignKey: 'goodsInfoId'
})

GoodsInfo.hasMany(DecoratItemInfo, {
  foreignKey: 'goodsInfoId'
})

DecoratItem.hasMany(DecoratItemInfo, {
  foreignKey: 'decoratItemId'
})

GoodsInfo.hasMany(GoodsPhoto, {
  foreignKey: 'goodsInfoId'
})

Account.hasMany(AccessRecord, {
  foreignKey: 'linkUserId'
})

Account.hasMany(RechargeRecord, {
  foreignKey: 'linkUserId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'houseOwnerId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'supervisionId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'foremanId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'bricklayerId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'carpenterId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'plumberId'
})

Account.hasMany(HouseInfo, {
  foreignKey: 'painterId'
})

Account.hasMany(AcceptanceInfo, {
  foreignKey: 'workerAccountId'
})

HouseInfo.hasMany(AcceptanceInfo, {
  foreignKey: 'houseInfoId'
})

module.exports = {
  User,
  Account,
  GoodsInfo,
  GoodsPhoto,
  Product,
  DecoratItem,
  DecoratItemInfo,
  AccessRecord,
  RechargeRecord,
  AcceptanceInfo,
  HouseInfo
}
